package com.tuxing.draa_data.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuxing.draa_data.dao.center.NewDcBibliographyPeriDao;
import com.tuxing.draa_data.dao.center.NewDcBibliographyPeriDetailDao;
import com.tuxing.draa_data.pojo.ChaoXingDiff;
import com.tuxing.draa_data.pojo.center.NewDcBibliographyPeri;
import com.tuxing.draa_data.pojo.center.NewDcBibliographyPeriDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lzb
 * @version 1.0
 * @className ChaoXingPeriServiceImpl
 * @description 基础描述:
 * @date 2021/12/1 16:10
 */
@Service
@Slf4j
public class NewDcBibliographyPeriImpl extends ServiceImpl<NewDcBibliographyPeriDao, NewDcBibliographyPeri> implements NewDcBibliographyPeriService {

    @Autowired
    private NewDcBibliographyPeriDao newDcBibliographyPeriDao;

    @Autowired
    private NewDcBibliographyPeriDetailDao newDcBibliographyPeriDetailDao;

    @Override
    public Integer findCount() {
        QueryWrapper<NewDcBibliographyPeri> queryWrapper = new QueryWrapper<>();
        return newDcBibliographyPeriDao.selectCount(queryWrapper);
    }

    @Override
    public List<NewDcBibliographyPeri> findLimit(Integer start,Integer end) {
        QueryWrapper<NewDcBibliographyPeri> queryWrapper = new QueryWrapper<>();
        StringBuffer buffer = new StringBuffer();
        buffer.append("limit ");
        buffer.append(start);
        buffer.append(" , ");
        buffer.append(end);
        queryWrapper.last(buffer.toString());
        return newDcBibliographyPeriDao.selectList(queryWrapper);
    }

    @Override
    public void saveAllData(List<ChaoXingDiff> cxDiffList) {
        log.info("开始插入NewDcBibliographyPeri和NewDcBibliographyPeriDetail数据");
        for(ChaoXingDiff chaoXingDiff : cxDiffList){
           try{
               NewDcBibliographyPeri ndbp = chaoXingDiff.getNdbp();
               NewDcBibliographyPeriDetail ndbpd = chaoXingDiff.getNdbpd();
               newDcBibliographyPeriDao.insert(ndbp);
               ndbpd.setRecordId(ndbp.getRecordId());
               newDcBibliographyPeriDetailDao.insert(ndbpd);
           }catch (Exception e){
               log.error("保存数据错误，错误原因：" + e.getMessage(), e);
               log.info("保存错误临时数据：" + JSON.toJSONString(chaoXingDiff));
           }
        }
        log.info("数据插入结束");
    }
}
